//
//  DestDetailViewController.m
//  RoadLine
//
//  Created by dllo on 15/4/21.
//  Copyright (c) 2015年 RedFireNoodles. All rights reserved.
//
//地点
#import "DestDetailViewController.h"
#import "BaseCollectionView.h"
#import "AFHTTPRequestOperationManager.h"
#import "ContryModel.h"
#import "CityModel.h"
#import "UIImageView+WebCache.h"
#import "TripListViewController.h"
#import "PlaceListViewController.h"
#import "Constants.h"
#import "NightManager.h"
#import "ViewController.h"
#import "Singleton.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface DestDetailViewController ()
// "地点"
@property(nonatomic, retain)NSMutableArray *destinationArray;
// 上面大图片
@property(nonatomic, retain)UIImageView *destImgView;
@property(nonatomic, retain)ContryModel *country;
@property(nonatomic, retain)CityModel *city;
@property(nonatomic, retain)UIActivityIndicatorView *activity;
@property(nonatomic, retain)UILabel *countryNameLbl;

@end

@implementation DestDetailViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.destinationArray = [NSMutableArray array];
        self.country = [ContryModel contryModel];
        self.city = [CityModel cityModel];
        self.activity = [[UIActivityIndicatorView alloc] init];
    }
    return self;
}

// 解析数据
- (void)parseData {
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *strURL = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.type, self.destId];
    [manager GET:strURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // 如果是国家(type = 1)
        if ([self.type integerValue] == 1) {
            [self.country setValuesForKeysWithDictionary:responseObject];
            // 赋值
            [self assignValue];
        } else {
            [self.city setValuesForKeysWithDictionary:responseObject];
            // 赋值
            [self assignValue];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 解析数据
    [self parseData];
    
    //修改UInavigationBar的字体颜色(系统API没有这个设置 只能自己建一个Label覆盖 然后修改label的属性)
    UILabel *titleLabelPrice = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    titleLabelPrice.font = [UIFont boldSystemFontOfSize:18];
    titleLabelPrice.textColor = [UIColor whiteColor];
    titleLabelPrice.backgroundColor = [UIColor clearColor];
    titleLabelPrice.text = @"      地点";
    self.navigationItem.titleView = [titleLabelPrice autorelease];
    
    // 相框UIImageView
    self.destImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT / 3)];
    [self.view addSubview:self.destImgView];
    self.destImgView.contentMode = UIViewContentModeScaleAspectFill;
    self.destImgView.autoresizesSubviews = YES;
    self.destImgView.clipsToBounds = YES;
   
    // 照片下面的背景图
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, HEIGHT / 3, WIDTH, HEIGHT *2/ 3)];
    imageView.image = [UIImage imageNamed:@"pink"];
    [self.view addSubview:imageView];
    imageView .contentMode = UIViewContentModeScaleAspectFill;
    imageView.autoresizesSubviews = YES;
    imageView.clipsToBounds = YES;
    
    // 小菊花
    self.activity.frame = CGRectMake(0, 0, WIDTH, HEIGHT / 3);
    [self.destImgView addSubview:self.activity];
    self.activity.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;
    [self.activity startAnimating];
    
    // 所在地的名字UILabel
    self.countryNameLbl = [[UILabel alloc] initWithFrame:CGRectMake(20, self.destImgView.frame.size.height + 20, WIDTH - 40, 60)];
    self.countryNameLbl.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
    self.countryNameLbl.backgroundColor = [UIColor clearColor];
    self.countryNameLbl.textColor = [UIColor whiteColor];
    self.countryNameLbl.textAlignment = NSTextAlignmentCenter;
    CGFloat marginHeight = self.destImgView.frame.size.height + 20 + 60;
    [self.view addSubview:self.countryNameLbl];
    
    // 底层的绿色View
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(30, marginHeight + 80, WIDTH - 60, 50)];
    view.layer.cornerRadius = 8;
    view.backgroundColor = [UIColor colorWithRed:241/255.0 green:63/255.0 blue:73/255.0 alpha:0.6];
    [self.view addSubview:view];
    
    // 所有旅行地点UIButton
    UIButton *showAllPlaceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    showAllPlaceBtn.frame = CGRectMake(30, marginHeight + 80, WIDTH - 60, 50);
    [showAllPlaceBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    showAllPlaceBtn.backgroundColor = [UIColor clearColor];
    [showAllPlaceBtn setTitle:kAllTripPlace forState:UIControlStateNormal];
    [showAllPlaceBtn addTarget:self action:@selector(pushToPlaceListVC:) forControlEvents:UIControlEventTouchUpInside];
    showAllPlaceBtn.tag = 10001;
    [self.view addSubview:showAllPlaceBtn];
    
    // 途行日记UIButton
    UIButton *roadLineDairyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    roadLineDairyBtn.frame = CGRectMake(30, marginHeight, WIDTH - 60, 50);
    roadLineDairyBtn.layer.cornerRadius = 8;
    [roadLineDairyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    roadLineDairyBtn.backgroundColor = [UIColor colorWithRed:176/255.0 green:222/255.0 blue:52/255.0 alpha:0.6];
    [self.view addSubview:roadLineDairyBtn];
    roadLineDairyBtn.tag = 10000;
    [roadLineDairyBtn setTitle:kRoadLineDairy forState:UIControlStateNormal];
    [roadLineDairyBtn addTarget:self action:@selector(pushToTripListVC:) forControlEvents:UIControlEventTouchUpInside];

    // 夜间模式的单例
    if ([Singleton shareSingleton].isNight) {
        [[Singleton shareSingleton] loadNightModeInfoFormDisk];
    }
    
    [_countryNameLbl release];
    [_destImgView release];
    [_activity release];
    
}
// 赋值
- (void)assignValue {
    // 获取主队列
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    // 把子线程里图片在主线程里显示
    dispatch_async(mainQueue, ^{
        if ([self.country.hottest_places count] != 0) {
            [self.destImgView sd_setImageWithURL:self.country.hottest_places[0][@"photo"] placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
        }
        // 对已经存在的图片进行赋值
        self.countryNameLbl.text = self.country.name;
        if (!self.country.name) {
            if ([self.city.hottest_places count] != 0) {
                [self.destImgView sd_setImageWithURL:self.city.hottest_places[0][@"photo"] placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
            } else {
                [self.destImgView sd_setImageWithURL:[NSURL URLWithString:self.city.icon] placeholderImage:[UIImage imageNamed:@"BEIJING.png"]];
            }

            self.countryNameLbl.text = self.city.name;
        }
        [self.activity stopAnimating];
    });
}

// 白天模式的颜色
- (void)setLightDayModel {
    self.view.backgroundColor = [UIColor colorWithRed:249/255.0 green:242/255.0 blue:226/255.0 alpha:1];
    UIButton *roadLineDairyBtn = (UIButton *)[self.view viewWithTag:10000];
    UIButton *showAllPlaceBtn = (UIButton *)[self.view viewWithTag:10001];
    roadLineDairyBtn.backgroundColor = [UIColor colorWithRed:176/255.0 green:222/255.0 blue:52/255.0 alpha:0.6];
    showAllPlaceBtn.backgroundColor = [UIColor clearColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:246/255.0 green:85/255.0 blue:91/255.0 alpha:1];
}

// 夜间模式的颜色
- (void)setNightDayModel {

    [NightManager setBackgroundColorWithView:self.view];
    UIButton *roadLineDairyBtn = (UIButton *)[self.view viewWithTag:10000];
    UIButton *showAllPlaceBtn = (UIButton *)[self.view viewWithTag:10001];
    roadLineDairyBtn.backgroundColor = [UIColor grayColor];
    showAllPlaceBtn.backgroundColor = [UIColor grayColor];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:4/255.0 green:165/255.0 blue:171/255.0 alpha:1];
}

- (void)pushToTripListVC:(UIButton *)btn {
    TripListViewController *tripLstVC = [[TripListViewController alloc] init];
    tripLstVC.tripID = self.country.countryId;
    tripLstVC.type = [NSString stringWithFormat:@"%ld", self.country.type];
    if (!self.country.countryId) {
        tripLstVC.tripID = [NSString stringWithFormat:@"%@", self.city.cityId];
        tripLstVC.type = [NSString stringWithFormat:@"%ld", self.city.type];
    }
    [self.navigationController pushViewController:tripLstVC animated:YES];
    [tripLstVC release];
}

- (void)pushToPlaceListVC:(UIButton *)btn {
    PlaceListViewController *placeListVC = [[PlaceListViewController alloc] init];
    placeListVC.placeId = self.country.countryId;
    placeListVC.type = [NSString stringWithFormat:@"%ld", self.country.type];
    placeListVC.locationDic = self.country.location;
    if (!self.country.countryId) {
        placeListVC.placeId = [NSString stringWithFormat:@"%@", self.city.cityId];
        placeListVC.type = [NSString stringWithFormat:@"%ld", self.city.type];
        placeListVC.locationDic = self.city.location;
    }
    [self.navigationController pushViewController:placeListVC animated:YES];
    [placeListVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc
{
    [_countryNameLbl release];
    [_destImgView release];
    [_destinationArray release];
    [_country release];
    [_activity release];
    [_city release];

    [super dealloc];
}

@end
